import json 
from flask import Flask, render_template, request
from utils import openai_handle 
import os

app = Flask(__name__)

os.environ["http_proxy"] = "http://127.0.0.1:7890"
os.environ["https_proxy"] = "http://127.0.0.1:7890"

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/create_img', methods=['GET','POST'])
def create_img():
    if request.method == 'POST':
        # 接受提交的数据
        query = request.form.get('query')
        img_size = request.form.get('img_size')
        # 请求openai
        result = openai_handle.create_images(query, img_size)
        # 判断是否为base64
        if 'data:image/png;base64,' not in result:
            return json.dumps({'status_code': 0, 'message': 'EROOR', 'content': result})
        return json.dumps({'status_code': 1, 'message': 'OK', 'content': result})
    return render_template('create_img.html')

@app.route('/debug', methods=['GET','POST'])
def debug():
    if request.method == 'POST':
        # 接受提交的数据
        query = request.form.get('query')
        result = openai_handle.fix_bug(query)
        return json.dumps({'status_code': 1, 'message': 'OK', 'content': result})
    return render_template('debug.html')


@app.route('/write_code', methods=['GET','POST'])
def write_code():
    if request.method == 'POST':
        # 接受提交的数据
        query = request.form.get('query')
        result = openai_handle.chat(query)
        return json.dumps({'status_code': 1, 'message': 'OK', 'content': result})
    return render_template('write_code.html')

if __name__  == "__main__":
    app.run(debug=True)

